package com.jiexin.controller;

import com.google.common.collect.Maps;
import com.jiexin.beans.PageQuery;
import com.jiexin.common.JsonData;
import com.jiexin.dao.SysUserMapper;
import com.jiexin.dto.AclModuleLevelDto;
import com.jiexin.model.SysRole;
import com.jiexin.model.SysUser;
import com.jiexin.param.AclModuleParam;
import com.jiexin.param.AclParam;
import com.jiexin.service.SysAclService;
import com.jiexin.service.SysRoleAclService;
import com.jiexin.service.SysTreeService;
import com.jiexin.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author: Jay_Liu
 * @Description: 权限点
 * @Date: Created in 13:23 2018/4/8 0008
 * @Modified By:
 */
@Controller
@RequestMapping("/sys/acl")
@Slf4j
public class SysAclController {

    @Resource
    SysAclService sysAclService;

    @Resource
    SysRoleAclService sysRoleAclService;

    @RequestMapping("/save.json")
    @ResponseBody
    //保存
    public JsonData saveAcl(AclParam aclParam) {
        sysAclService.save(aclParam);
        return JsonData.success();
    }

    @RequestMapping("/update.json")
    @ResponseBody
    //更新
    public JsonData updateAcl(AclParam aclParam) {
        sysAclService.update(aclParam);
        return JsonData.success();
    }

    @RequestMapping("/page.json")
    @ResponseBody
    //权限点分页数据
    public JsonData list(@Param("aclModuleId") Integer aclModuleId, PageQuery pageQuery) {
        return JsonData.success(sysAclService.getPageByAclModuleId(aclModuleId, pageQuery));
    }

    @RequestMapping("/rolesAndUsers.json")
    @ResponseBody
    public JsonData rolesAndUsers(@RequestParam("aclId") Integer aclId) {
        //获取该权限关联的角色列表
        List<SysRole> roleList = sysRoleAclService.getRoleListByAclId(aclId);
        //获取该用户所有的角色
        List<SysUser> userList = sysRoleAclService.getUserListByAclId(aclId);
        //Map结果
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("roles", roleList);
        resultMap.put("users", userList);
        return JsonData.success(resultMap);
    }

}
